<?php

/**
 * 
 * @version $Id: ReservaController.php 17 2012-06-18 00:34:17Z joaopaulopvillela@gmail.com $
 */
class Default_ReservaController extends Zend_Controller_Action {

    /**
     * 
     * @var Default_Form_Reserva
     */
    protected $_form;

    /**
     * 
     * @var Zend_Config_Ini
     */
    protected $_config;

    /**
     * 
     * @var Model_Mapper_Reserva
     */
    protected $_mapper;

    /**
     * (non-PHPdoc)
     * @see Zend_Controller_Action::init()
     */
    public function init() {
        $this->_mapper = new Model_Mapper_Reserva();
        $this->_config = Zend_Registry::get('config');
        $this->_form = $this->_getForm($this->_helper->url('save'));
    }

    /**
     * 
     * @access 	public
     * @param 	string $action
     * @return 	Default_Form_Reserva
     */
    protected function _getForm($action) {
        $form = new Default_Form_Reserva(
                        array(
                            'action' => $action,
                            'method' => Zend_Form::METHOD_POST,
                            'id' => 'form-reserva',
                            'onSubmit' => 'return reserva();'
                        )
        );

        return $form;
    }

    /**
     * 
     * @access public
     * @return void
     */
    public function indexAction() {
        $this->view->form = $this->_form;
    }

    /**
     *
     * @access public
     * @return void
     */
    public function saveAction() {
        if ($this->getRequest()->isPost()) {

            if ($this->_form->isValid($this->getRequest()->getPost())) {

                $result = array(
                    'valid' => $this->_mapper->send($this->_form->getValues()),
                    'message' => $this->_mapper->getMessage()->toArray()
                );
            } else {

                $message = new App_Message();

                $message->addMessage($this->_config->messages->warning, App_Message::WARNING);

                $result = array(
                    'valid' => false,
                    'message' => $message->toArray(),
                    'error' => $this->_form->getMessages()
                );
            }

            $this->_helper->json($result);
        }

        $this->_helper->redirector->gotoSimple('index');
    }

}